//
//  PopTransitionAnimation.m
//  29-tableView_Animation
//
//  Created by Snow on 16/8/23.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "PopTransitionAnimation.h"
#import "ViewController.h"
#import "SecondViewController.h"
@implementation PopTransitionAnimation
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 1.0f;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    ViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    SecondViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:toVC.view];
    [toVC.tableView cellForRowAtIndexPath:toVC.index].hidden = YES;
    UIView *snapview = [fromVC.imageOfSecond snapshotViewAfterScreenUpdates:NO];
    snapview.frame = fromVC.imageOfSecond.frame;
    [containerView addSubview:snapview];
    [containerView addSubview:fromVC.tableview];
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        snapview.frame = toVC.cgrect;
    } completion:^(BOOL finished) {
        [toVC.tableView cellForRowAtIndexPath:toVC.index].hidden = NO;
        [snapview removeFromSuperview];
        [transitionContext completeTransition:YES];
    }];
}

@end
